/**
 * Component for server starup.
 */
var Server = require('../server/server');
//该组件其实是../server/server.js的包装
/**
 * Component factory function
 * 工厂函数定义
 * @param {Object} app  current application context
 * @return {Object}     component instance
 */
module.exports = function(app, opts) {
	return new Component(app, opts);
};

/**
 * Server component class
 * server组件
 * @param {Object} app  current application context
 */
var Component = function(app, opts) {
	this.server = Server.create(app, opts);//实际做工作的类
};

var pro = Component.prototype;

pro.name = '__server__';//组件的名字

/**
 * Component lifecycle callback
 * 组件生命周期回调
 * @param {Function} cb
 * @return {Void}
 */
pro.start = function(cb) {
	this.server.start();//调用实际server的启动函数
    process.nextTick(cb);//在下一个循环中调用回调函数 继续初始化组件，这样就不用在实际server里调用回调函数了，好主意
};

/**
 * Component lifecycle callback
 * 组件生命周期回调
 * @param {Function} cb
 * @return {Void}
 */
//afterStart定义 类似start
Component.prototype.afterStart = function(cb) {
	this.server.afterStart();
	process.nextTick(cb);
};

/**
 * Component lifecycle function
 * 组件生命周期回调
 * @param {Boolean}  force whether stop the component immediately
 * @param {Function}  cb
 * @return {Void}
 */
//stop定义 类似start
pro.stop = function(force, cb) {
	this.server.stop();
	process.nextTick(cb);
};

/**
 * Proxy server handle
 * 代理实际server的handle函数
 */
pro.handle = function(msg, session, cb) {
	this.server.handle(msg, session, cb);
};

/**
 * Proxy server global handle
 * 代理实际server的全局handle函数
 */
Component.prototype.globalHandle = function(msg, session, cb) {
	this.server.globalHandle(msg, session, cb);
};